//
//  ComicReadViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/20.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "ComicReadViewController.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "XZBaseNetwork.h"

//宏定义高度与宽度
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ComicReadViewController ()<UIImagePickerControllerDelegate,UIScrollViewDelegate>

@end

@implementation ComicReadViewController
{
    NSArray* _dataSource;
    UIScrollView* _comicShowView;
    UILabel* _tagLabel;
    NSTimer* _updateDate;
    NSDate* _currentTime;
    CGFloat _currentPage;

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://japi.juhe.cn/comic/chapterContent" params:
     
  @{@"comicName":self.comicName,
    @"id":[NSNumber numberWithInt:self.comicId],
    @"key":@"e4c2c6f92b486c97e01328b1bc54c913"} success:^(id success) {
        
        _dataSource = [[success objectForKey:@"result"] objectForKey:@"imageList"];
        NSLog(@"_dasouceForComic:%@",_dataSource);
        [_comicShowView reloadInputViews];
        [self loadComicReadView];
        [self.view addSubview:_comicShowView];
        [self.view addSubview:[self tagInfo]];
    } failure:^(id failure) {
        
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.alpha = 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.alpha = 1;
    self.navigationItem.title = _comicChapter;
    //点击手势,点击屏幕显示出导航栏,再点一次导航栏消失
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action:)];
    [self.view addGestureRecognizer:tap];
    
    _updateDate = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(updateDate:) userInfo:nil repeats:YES];
    
    
}

- (void)updateDate:(NSTimer* )time
{
    _currentTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString* dateStr = [formatter stringFromDate:_currentTime];
    _tagLabel.text = [NSString stringWithFormat:@"%@  %.0f/%ld  %@",_comicChapter,_currentPage,_dataSource.count,dateStr];
    
}

#pragma mark 视图的懒加载

- (UIScrollView* )loadComicReadView
{
    if (_comicShowView == nil) {
        _comicShowView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        /*
         
            这里预留的的contentSize是根据获取图片的数量数组的count来进行变化的
            
         
         */
        _comicShowView.contentSize = CGSizeMake(WIDTH* _dataSource.count, 0);
        _comicShowView.pagingEnabled = YES;
        _comicShowView.delegate = self;
        for (NSInteger i = 0 ; i < _dataSource.count; i++) {
            UIImageView* cacheImage = [[UIImageView alloc]initWithFrame:CGRectMake(i * WIDTH, 20, WIDTH, HEIGHT-49)];
            [cacheImage sd_setImageWithURL:[[_dataSource objectAtIndex:i] objectForKey:@"imageUrl"] placeholderImage:[UIImage imageNamed:@"backView.jpg"]];
            cacheImage.userInteractionEnabled = YES;
            
            [_comicShowView addSubview:cacheImage];
            NSLog(@"循环次数:%ld",i);
        }
        
        
        
        
    }
    return _comicShowView;
}

- (UILabel* )tagInfo
{
    if (_tagLabel == nil) {
        
        _currentTime = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"HH:mm"];
        NSString* dateStr = [formatter stringFromDate:_currentTime];
        _tagLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2 + 50, HEIGHT - 20, 200, 20)];
        _tagLabel.backgroundColor = [UIColor darkTextColor];
        _tagLabel.textColor = [UIColor whiteColor];
        _tagLabel.text = [NSString stringWithFormat:@"%@  1/%ld  %@",_comicChapter,_dataSource.count,dateStr];
        _tagLabel.font = [UIFont systemFontOfSize:12.0];
    }
    return _tagLabel;
}

- (void)action:(UITapGestureRecognizer* )sender
{
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
    else{
        self.navigationController.navigationBar.hidden = YES;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _currentTime = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString* dateStr = [formatter stringFromDate:_currentTime];
    
    _currentPage = scrollView.contentOffset.x / WIDTH;
    _tagLabel.text = [NSString stringWithFormat:@"%@   %.0f/%ld  %@",_comicChapter,_currentPage + 1,_dataSource.count,dateStr];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
